package com.lepu.app.fun.chat.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import util.DateUtil;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.eyzhs.app.R;
import com.lepu.app.fun.chat.bean.ChatSession;
import com.lepu.app.fun.chat.util.BadgeView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import common.db.Constants;

/**
 * Created by lepu on 2014/12/20.
 */
public class ChatSessionAdapter extends BaseAdapter {
    Context context;
    List<ChatSession> list;
    ImageLoader imageLoader;

    public ChatSessionAdapter(Context context, List<ChatSession> list, ImageLoader imageLoader) {
        this.context = context;
        this.list = list;
        this.imageLoader = imageLoader;
    }

    public void clearAll() {
        list.clear();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        viewHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.chat_item_chat_session_list, null);
            holder = new viewHolder();
            holder.relative_card = (FrameLayout) view.findViewById(R.id.relative_card);
            holder.header = (ImageView) view.findViewById(R.id.imgView_user_head);
            holder.txtview_category = (ImageView) view.findViewById(R.id.txtview_category);
            holder.name = (TextView) view.findViewById(R.id.txt_user_name);
            holder.content = (TextView) view.findViewById(R.id.txt_request);
            holder.date = (TextView) view.findViewById(R.id.txt_time);
            holder.newMsg = (BadgeView) view.findViewById(R.id.textview_tips);
            holder.newMsg.setBackground(9, context.getResources().getColor(R.color.colortipsred));
            view.setTag(holder);
        } else {
            holder = (viewHolder) view.getTag();
        }

        ChatSession chatSession = list.get(i);
        holder.name.setText(chatSession.getUserNickName());

        Date time = chatSession.getLastMsgTime() == null ? new Date() : chatSession.getLastMsgTime();
        String data = DateUtil.formatTime(time);

        holder.date.setText(data);

        if (chatSession.getNewMsgCount() > 999) {
            chatSession.setNewMsgCount(999);
        }
//        if (chatSession.getLastMsgContent().equals(Constants.Counts.NEW_PATIENT_FLAG)) {
//            holder.content.setText("");
//            holder.newMsg.setVisibility(View.VISIBLE);
//            holder.newMsg.setText("1");
//        } else {
//            holder.content.setText(chatSession.getQuestion());
//            if (chatSession.getNewMsgCount() != 0) {
//                holder.newMsg.setVisibility(View.VISIBLE);
//                holder.newMsg.setText(chatSession.getNewMsgCount() + "");
//            } else {
//                holder.newMsg.setText("0");
//                holder.newMsg.setVisibility(View.GONE);
//            }
//        }

        holder.content.setText(chatSession.getQuestion());
        if (chatSession.getNewMsgCount() != 0) {
            holder.newMsg.setVisibility(View.VISIBLE);

            holder.newMsg.setText(chatSession.getNewMsgCount() + "");
        } else {
            holder.newMsg.setText("0");
            holder.newMsg.setVisibility(View.GONE);
        }
        if (chatSession.getUserProfilePictureId() != null && !TextUtils.isEmpty(chatSession.getUserProfilePictureId())) {
            ImageLoader.getInstance().displayImage(chatSession.getUserProfilePictureId(), holder.header, getOption());
        } else {
            holder.header.setImageResource(R.drawable.default_doctor);
        }
        if (chatSession != null && holder != null && holder.newMsg != null) {
            holder.newMsg.setTag(chatSession.getQuestionID());
        }
        return view;
    }

    class viewHolder {
        ImageView header, txtview_category;
        TextView name, content, date;
        BadgeView newMsg;
        FrameLayout relative_card;
    }

    private DisplayImageOptions getOption() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.default_doctor)
                .showImageForEmptyUri(R.drawable.default_doctor)
                .showImageOnFail(R.drawable.default_doctor)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)
                .build();
        return options;
    }
}
